﻿using Siasun.AFC.AfcCommonLib.Base;
using Siasun.AFC.Common;
using Siasun.AFC.RtPlatfromGui.Dao.Define;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Siasun.AFC.RtPlatfromGui.Dao.Device
{
    /// <summary>
    /// 数据链路层
    /// </summary>
    public class RtMenuDataDao
    {
        /// <summary>
        /// 查询时间段内的未上传的全部更换记录
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime">  </param>
        /// <returns></returns>
        public List<RtMenuDataModel> QueryAllMenuData()
        {
            FirebirdHelper fDb = new FirebirdHelper(DevPathDefine.DB_DEVICE);
            if (File.Exists(DevPathDefine.DB_DEVICE))
            { 
                
            }
            try
            {
                if (!fDb.Open())
                {
                    return null;
                }

                List<RtMenuDataModel> modelList = new List<RtMenuDataModel>();
                String sql = string.Format("select first 1000 skip 0 * from RT_MENU_DATA t;");
                //获取列名和数值的方法
                List<Dictionary<string, string>> rData = fDb.ExecuteReadDetial(sql);

                if (!object.Equals(null, rData) && rData.Count > 0)
                {
                    modelList = RtMenuDataModel.ConvertDataArray(rData);
                    fDb.Close();
                    //返回实体类集合
                    return !object.Equals(null, modelList) ? modelList : null;
                }
                fDb.Close();
                return null;
            }
            catch (Exception ex)
            {
                LoggerInf.Info("查询 RT_MENU_DATA 信息异常!" + ex.Message.ToString());
                fDb.Close();
                return null;
            }
        }

        /// <summary>
        /// 根据ID查询菜单信息
        /// </summary>
        /// <param name="dataId"></param>
        /// <returns></returns>
        public RtMenuDataModel GetMenuDataById(string dataId)
        {
            FirebirdHelper fDb = new FirebirdHelper(DevPathDefine.DB_DEVICE);
            try
            {
                if (!fDb.Open())
                {
                    return null;
                }

                RtMenuDataModel model = new RtMenuDataModel();
                String sql = string.Format("select * from RT_MENU_DATA t where t.ID = '$1$';");
                sql = sql.Replace("$1$", dataId);
                //获取列名和数值的方法
                List<Dictionary<string, string>> rData = fDb.ExecuteReadDetial(sql);

                if (!object.Equals(null, rData) && rData.Count > 0)
                {
                    model = RtMenuDataModel.ConvertData(rData[0]);
                    fDb.Close();
                    //返回实体类集合
                    return !object.Equals(null, model) ? model : null;
                }
                fDb.Close();
                return null;
            }
            catch (Exception ex)
            {
                LoggerInf.Info("查询 RT_MENU_DATA 信息异常!" + ex.Message.ToString());
                fDb.Close();
                return null;
            }
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="changeBoxModel"></param>
        /// <returns></returns>
        public bool UpdateData(RtMenuDataModel dateModel)
        {
            if (object.Equals(null, dateModel))
            {
                return false;
            }
            FirebirdHelper fDb = new FirebirdHelper(DevPathDefine.DB_DEVICE);
            try
            {
                if (!fDb.Open())
                {
                    return false;
                }
                string sql = "update RT_MENU_DATA t "
                            + " set t.PARENT_MENU_ID = '$1$', t.MENU_NAME = '$2$',"
                            + " t.DLL_NAME = '$3$', t.DLL_FILE_PATH = '$4$',"
                            + " t.NAMESPACE_PATH = '$5$', t.MENU_PAGE_NAME = '$6$',"
                            + " t.MENU_ISABLE = '$7$', t.MENU_ISSHOW = '$8$', t.MENU_ISSHOWDIALOG = '$9$',"
                            + " t.MENU_MEDIFY_TIME = CURRENT_TIMESTAMP, t.NODE_TYPE = '$10$'"
                            + " where t.Id = '$0$';";
                sql = sql.Replace("$0$", dateModel.Id.ToString());
                sql = sql.Replace("$1$", dateModel.ParentMenuId);
                sql = sql.Replace("$2$", dateModel.MenuName);
                sql = sql.Replace("$3$", dateModel.DllName);
                sql = sql.Replace("$4$", dateModel.DllFilePath);
                sql = sql.Replace("$5$", dateModel.NamespacePath);
                sql = sql.Replace("$6$", dateModel.MenuPageName);
                sql = sql.Replace("$7$", dateModel.MenuIsable.ToString());
                sql = sql.Replace("$8$", dateModel.MenuIsShow.ToString());
                sql = sql.Replace("$9$", dateModel.MenuIsShowDialog.ToString());
                sql = sql.Replace("$10$", dateModel.NodeType.ToString());

                if (fDb.Execute(sql))
                {
                    fDb.Close();
                    LoggerInf.Info("更新数据库成功！更新Id" + dateModel.Id.ToString());
                    return true;
                }
                fDb.Close();
                LoggerInf.Info("更新数据库失败！");
                return false;
            }
            catch (Exception ex)
            {
                fDb.Close();
                LoggerInf.Info("更新数据库异常！" + ex.Message.ToString());
                return false;
            }
        }

        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="changeBoxModel"></param>
        /// <returns></returns>
        public bool InsertData(RtMenuDataModel dateModel)
        {
            if (object.Equals(null, dateModel))
            {
                return false;
            }
            FirebirdHelper fDb = new FirebirdHelper(DevPathDefine.DB_DEVICE);
            try
            {
                if (!fDb.Open())
                {
                    return false;
                }
                //将实体类信息变为语句
                string insertSql = "insert into RT_MENU_DATA "
                    + " (PARENT_MENU_ID, MENU_NAME, DLL_NAME, DLL_FILE_PATH, NAMESPACE_PATH, MENU_PAGE_NAME, NODE_TYPE"
                    + " MENU_ISABLE, MENU_ISSHOW, MENU_ISSHOWDIALOG) "
                    + " values"
                    + " ('$2$', '$3$', '$4$', '$5$', '$6$', '$7$', '$8$', '$9$', '$10$')";
                //insertSql = insertSql.Replace("$1$", dateModel.Id);//触发器，递增
                insertSql = insertSql.Replace("$2$", dateModel.ParentMenuId);
                insertSql = insertSql.Replace("$3$", dateModel.DllName);
                insertSql = insertSql.Replace("$4$", dateModel.DllFilePath);
                insertSql = insertSql.Replace("$5$", dateModel.NamespacePath);
                insertSql = insertSql.Replace("$6$", dateModel.MenuPageName);
                insertSql = insertSql.Replace("$7$", dateModel.MenuIsable.ToString());
                insertSql = insertSql.Replace("$8$", dateModel.MenuIsShow.ToString());
                insertSql = insertSql.Replace("$9$", dateModel.MenuIsShowDialog.ToString());
                insertSql = insertSql.Replace("$10$", dateModel.NodeType.ToString());

                if (fDb.Execute(insertSql))
                {
                    fDb.Close();
                    LoggerInf.Info("插入数据库成功！");
                    return true;
                }
                fDb.Close();
                LoggerInf.Info("插入数据库失败！");
                return false;
            }
            catch (Exception ex)
            {
                fDb.Close();
                LoggerInf.Info("插入数据库异常！" + ex.Message.ToString());
                return false;
            }
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="dateModel"></param>
        /// <returns></returns>
        public bool DeleteData(RtMenuDataModel dateModel)
        {
            if (object.Equals(null, dateModel))
            {
                return false;
            }
            FirebirdHelper fDb = new FirebirdHelper(DevPathDefine.DB_DEVICE);
            try
            {
                if (!fDb.Open())
                {
                    return false;
                }
                //将实体类信息变为语句
                string sql = "Delete from RT_MENU_DATA t where t.Id = '$1$'";
                sql = sql.Replace("$1$", dateModel.Id);

                if (fDb.Execute(sql))
                {
                    fDb.Close();
                    LoggerInf.Info("插入数据库成功！");
                    return true;
                }
                fDb.Close();
                LoggerInf.Info("插入数据库失败！");
                return false;
            }
            catch (Exception ex)
            {
                fDb.Close();
                LoggerInf.Info("插入数据库异常！" + ex.Message.ToString());
                return false;
            }
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="dataIdList"></param>
        /// <returns></returns>
        public bool DeleteData(List<string> dataIdList)
        {
            if (dataIdList.Count<=0 && dataIdList.Count>1000)
            {
                return false;
            }
            FirebirdHelper fDb = new FirebirdHelper(DevPathDefine.DB_DEVICE);
            try
            {
                if (!fDb.Open())
                {
                    return false;
                }
                //将实体类信息变为语句
                string sql = "Delete from RT_MENU_DATA t where t.Id in('$1$') ";
                string tempArrayStr = string.Empty;
                for (int i = 0; i < dataIdList.Count; i++)
                {
                    tempArrayStr += "'" + dataIdList[i] + "',";
                }
                sql = sql.Replace("$1$", tempArrayStr.TrimEnd(','));

                if (fDb.Execute(sql))
                {
                    fDb.Close();
                    LoggerInf.Info("插入数据库成功！");
                    return true;
                }
                fDb.Close();
                LoggerInf.Info("插入数据库失败！");
                return false;
            }
            catch (Exception ex)
            {
                fDb.Close();
                LoggerInf.Info("插入数据库异常！" + ex.Message.ToString());
                return false;
            }
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="dataId"></param>
        /// <returns></returns>
        public bool DeleteData(string dataId)
        {
            if (string.IsNullOrEmpty(dataId))
            {
                return false;
            }
            FirebirdHelper fDb = new FirebirdHelper(DevPathDefine.DB_DEVICE);
            try
            {
                if (!fDb.Open())
                {
                    return false;
                }
                //将实体类信息变为语句
                string sql = "Delete from RT_MENU_DATA t where t.Id = '$1$'";
                sql = sql.Replace("$1$", dataId);

                if (fDb.Execute(sql))
                {
                    fDb.Close();
                    LoggerInf.Info("插入数据库成功！");
                    return true;
                }
                fDb.Close();
                LoggerInf.Info("插入数据库失败！");
                return false;
            }
            catch (Exception ex)
            {
                fDb.Close();
                LoggerInf.Info("插入数据库异常！" + ex.Message.ToString());
                return false;
            }
        }
    }
}
